package com.tarry.controller;

import com.tarry.common.Result;
import com.tarry.model.dto.DeptLevelDto;
import com.tarry.model.vo.DeptPVo;
import com.tarry.service.SysDeptService;
import com.tarry.service.SysTreeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/sys/dept")
public class SysDeptController {

    @Autowired
    private SysDeptService sysDeptService;

    @Autowired
    private SysTreeService sysTreeService;

    @RequestMapping("/dept.do")
    public ModelAndView page() {
        return new ModelAndView("dept");
    }

    @RequestMapping("/save.json")
    @ResponseBody
    public Result saveDept(DeptPVo param) {
        sysDeptService.save(param);
        return Result.success();
    }

    @RequestMapping("/tree.json")
    @ResponseBody
    public Result tree() {
        List<DeptLevelDto> dtoList = sysTreeService.deptTree();
        return Result.success(dtoList);
    }

    @RequestMapping("/update.json")
    @ResponseBody
    public Result updateDept(DeptPVo param) {
        sysDeptService.update(param);
        return Result.success();
    }

    @RequestMapping("/delete.json")
    @ResponseBody
    public Result delete(@RequestParam("id") int id) {
        sysDeptService.delete(id);
        return Result.success();
    }

}
